<template>
  <div v-if="!state.loading && state.lotteries.length < 1" class="empty">
    <n-empty description="空空如也~"></n-empty>
  </div>
  <div v-if="state.loading" class="loading">
    <n-spin size="large" stroke="#c04851" />
  </div>
  <div v-if="state.lotteries.length > 0" class="list">
    <div v-for="(item, index) in state.lotteries" title="抽奖">
      <div v-if="item.state > 0" class="item">
        <h2>{{ index + 1 }}-{{ item.name }}</h2>
        <span class="spec">截至时间：{{ moment(item.end_at).format('YYYY-MM-DD HH:mm:ss') }}</span>
        <span class="spec">说明：{{ item.spec }}</span>
        <span class="spec">奖池：{{ item.pool }}</span>
        <!-- :disabled="item.state !== 1" -->
        <n-button @click="goto(`/shuffle/${item.id}`)" type="success" ghost class="mt-10">GO</n-button>
        <div class="result">
          <router-link class="link" :to="`/dashboard/${item.id}`">查看结果</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useMessage, NButton, NSpin, NEmpty } from 'naive-ui'
import axios from 'axios'
import moment from 'moment'
import { ApiPrefix } from '../../config.js'

const state = reactive({
  lotteries: [],
  loading: false
})


const router = useRouter()
const message = useMessage()

const goto = (e) => {
  router.push(e)
}

onMounted(() => {
  state.loading = true
  axios.post(`${ApiPrefix}/lottery/getAll`)
    .then((r) => {
      if (r.data && r.data.length > 0) state.lotteries = r.data
    })
    .catch((e) => {
      console.log(e)
      message.error('获取失败，请检查网络或稍后再试')
    })
    .finally(() => {
      state.loading = false
    })
})
</script>

<style scoped>
.empty,
.loading {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 282px;
}
.list {
  display: flex;
  flex-wrap: wrap;
  padding: 10px;
  width: 100%;
}
.item {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 294px;
  height: 320px;
  margin: 10px;
  padding: 20px;
  position: relative;
  background: #fff;
  box-sizing: border-box;
  border-radius: 16px;
  background-clip: padding-box;
  border: solid 2px transparent;
}
.item:before {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: -1;
  margin: -2px;
  border-radius: inherit;
  background: linear-gradient(
    236.27deg,
    #3fcfb7 9.64%,
    #4c5cb8 61.09%,
    #3a99ff 106.16%
  );
}
.item h2 {
  margin: 0;
}
.spec {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.result {
  display: flex;
  width: 100%;
  justify-content: center;
}
.result .link {
  font-size: 12px;
  color: #c4d7d6;
}
.mt-10 {
  margin-top: 10px;
}
@media screen and (max-width: 700px) {
  .list {
    justify-content: center;
  }
}
</style>